import Vue from 'vue'
import Vuex from 'vuex'
import WebStorage from '../service/WebStorage'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        corpName: '',
        logoPath: '',
        domain: '',
        cartCount: 0,
        categoryList: [],
        currentClassify: {
            id: -1,
            name: '全部分类',
        },
    },
    mutations: {
        init (state, { basic, main_category: mainCategory }) {
            const { categoryVOList, corpName, logoPath, domainKey } = basic
            state.categoryList = categoryVOList
            state.corpName = corpName
            state.logoPath = logoPath
            state.domain = domainKey
            if (mainCategory) {
                categoryVOList.map((item) => {
                    if (item.categoryId === mainCategory) {
                        state.currentClassify = {
                            id: item.categoryId,
                            name: item.name,
                        }
                    }
                })
            }
        },
        refreshCart (state) {
            state.cartCount = WebStorage.getCart().length
        },
        setClassify (state, payload) {
            state.currentClassify = payload
        },
    },
})

export default store